﻿using System;
using System.Data;
using System.Data.SqlClient;
using LoverMeeting.Model.Entity;

namespace LoverMeeting.DataBase.DataMappers
{
    public class LoverMapper : IDataMapper<Lover>
    {
        public Lover GetData(IDataReader reader)
        {
            var entity = new Lover
                              {
                                  Id = int.Parse(reader["LoverId"].ToString()),
                                  Nickname = reader["Nickname"].ToString(),
                                  Email = reader["Email"].ToString(),
                                  Birthday = DateTime.Parse(reader["Birthday"].ToString())
                              };
            if (!(reader["GenderId"] is DBNull))
                entity.Gender = new GenderMapper().GetData(reader);
            if (!(reader["CityId"] is DBNull))
            {
                entity.City = new CityMapper().GetData(reader);
                entity.City.Province = new ProvinceMapper().GetData(reader);
            }
            if (!(reader["LoverAccountId"] is DBNull))
                entity.Account = new LoverAccountMapper().GetData(reader);
            if (!(reader["LoverConfigurationId"] is DBNull))
            {
                entity.Configuration = new LoverConfigurationMapper().GetData(reader);
                entity.Configuration.DesiredGender = new DesiredGenderMapper().GetData(reader);
                entity.Configuration.Weight = new LoverWeightMapper().GetData(reader);
                entity.Configuration.Height = new LoverHeightMapper().GetData(reader);
                entity.Configuration.MaritalStatus = new MaritalStatusMapper().GetData(reader);
                entity.Configuration.RelationshipType = new RelationshipTypeMapper().GetData(reader);
            }
            if (!(reader["ImageId"] is DBNull))
                entity.Avatar = new ImageMapper().GetData(reader);
            return entity;
        }

        public void SetParameter(SqlCommand command, Lover entity)
        {
            command.Parameters.AddWithValue("LoverId", entity.Id);
            command.Parameters.AddWithValue("Nickname", entity.Nickname);
            command.Parameters.AddWithValue("Password", entity.Password);
            if(entity.Birthday > DateTime.MinValue)
                command.Parameters.AddWithValue("Birthday", entity.Birthday);
            command.Parameters.AddWithValue("Email", entity.Email);
            if(entity.Gender != null && entity.Gender.Id > 0)
                command.Parameters.AddWithValue("GenderId", entity.Gender);
            else
                command.Parameters.AddWithValue("GenderId", DBNull.Value);
            if(entity.City != null && entity.City.Id > 0)
                command.Parameters.AddWithValue("CityId", entity.City.Id);
            else
                command.Parameters.AddWithValue("CityId", DBNull.Value);
            if(entity.Configuration != null && entity.Configuration.Id > 0)
                command.Parameters.AddWithValue("LoverConfigurationId", entity.Configuration.Id);
            else
                command.Parameters.AddWithValue("LoverConfigurationId", DBNull.Value);
            if(entity.Account != null && entity.Account.Id > 0)
                command.Parameters.AddWithValue("LoverAccountId", entity.Account.Id);
            else
                command.Parameters.AddWithValue("LoverAccountId", DBNull.Value);
        }

        public int GetRecordCount(IDataReader reader)
        {
            return 0;
        }
    }
}
